// pages/editAddr/editAddr.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressId: '',
    default: true,
    region: [],
    mobile: '',
    name: '',
    address: ''
  },

  addAddr() {
    let data = this.data
    if (data.region == '' || data.name == '' || data.mobile == '' || data.address == '') {
      wx.showToast({
        title: '请输入完整',
        icon: 'none'
      })
      return
    }
    wx.$https('address.add', {
      addressId: data.addressId,
      name: data.name,
      mobile: data.mobile,
      address: data.address,
      selected: data.default ? 1 : 0,
      province: data.region[0],
      city: data.region[1],
      area: data.region[2]
    }).then(res => {
      wx.showToast({
        title: '成功',
        mask: 'true'
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1000)
    })
  },

  delete_(e) {
    if (e.detail.type == 0) return
    wx.$https('address.del', {
      addressId: this.data.addressId
    }).then(res => {
      wx.showToast({
        title: '删除成功！',
        mask: 'true'
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1000)
    })
  },

  delete() {
    this.delPop.hideShow({
      title: '删除',
      info: '删除的地址不可恢复，确认继续吗？'
    })
  },

  detail() {
    wx.$https('address.desc', {
      addressId: this.data.addressId
    }).then(res => {
      this.setData({
        ...res,
        region: [res.province, res.city, res.area],
        default: res.selected == 1 ? true : false
      })
    })
  },

  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },

  bindInp(e) {
    let value = e.detail.value
    let name = e.currentTarget.dataset.name
    this.setData({
      [`${name}`]: value
    })
  },

  change(e) {
    this.setData({
      default: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let addressId = options.addressId
    this.setData({
      addressId: addressId
    })
    console.log(options)
    if (addressId != '') this.detail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.delPop = this.selectComponent('#del')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})